package cn.iocoder.an.module.mold.controller.admin.recheckitem;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.recheckitem.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.recheckitem.RecheckItemDO;
import cn.iocoder.an.module.mold.service.recheckitem.RecheckItemService;

@Tag(name = "管理后台 - 模具复检项目")
@RestController
@RequestMapping("/mold/recheck-item")
@Validated
public class RecheckItemController {

    @Resource
    private RecheckItemService recheckItemService;

    @PostMapping("/create")
    @Operation(summary = "创建模具复检项目")
    @PreAuthorize("@ss.hasPermission('mold:recheck-item:create')")
    public CommonResult<Long> createRecheckItem(@Valid @RequestBody RecheckItemSaveReqVO createReqVO) {
        return success(recheckItemService.createRecheckItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具复检项目")
    @PreAuthorize("@ss.hasPermission('mold:recheck-item:update')")
    public CommonResult<Boolean> updateRecheckItem(@Valid @RequestBody RecheckItemSaveReqVO updateReqVO) {
        recheckItemService.updateRecheckItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具复检项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:recheck-item:delete')")
    public CommonResult<Boolean> deleteRecheckItem(@RequestParam("id") Long id) {
        recheckItemService.deleteRecheckItem(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具复检项目")
                @PreAuthorize("@ss.hasPermission('mold:recheck-item:delete')")
    public CommonResult<Boolean> deleteRecheckItemList(@RequestParam("ids") List<Long> ids) {
        recheckItemService.deleteRecheckItemListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具复检项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:recheck-item:query')")
    public CommonResult<RecheckItemRespVO> getRecheckItem(@RequestParam("id") Long id) {
        RecheckItemDO recheckItem = recheckItemService.getRecheckItem(id);
        return success(BeanUtils.toBean(recheckItem, RecheckItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具复检项目分页")
    @PreAuthorize("@ss.hasPermission('mold:recheck-item:query')")
    public CommonResult<PageResult<RecheckItemRespVO>> getRecheckItemPage(@Valid RecheckItemPageReqVO pageReqVO) {
        PageResult<RecheckItemDO> pageResult = recheckItemService.getRecheckItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecheckItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具复检项目 Excel")
    @PreAuthorize("@ss.hasPermission('mold:recheck-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecheckItemExcel(@Valid RecheckItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecheckItemDO> list = recheckItemService.getRecheckItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具复检项目.xls", "数据", RecheckItemRespVO.class,
                        BeanUtils.toBean(list, RecheckItemRespVO.class));
    }


    @GetMapping("/all")
    @Operation(summary = "获得模具保养分类项目对照")
    public CommonResult<List<RecheckItemDO>> getRecheckCat2itemByCatId() {
        List<RecheckItemDO> list = recheckItemService.getRecheckItemAll();
        return success(BeanUtils.toBean(list, RecheckItemDO.class));
    }

}